package com.skycode7.servlet;

import com.skycode7.bean.NewsBean;
import com.skycode7.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/news.html")
public class NewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        NewsDao newsDao = new NewsDao();
        String sp = req.getParameter("page");
        int page = 1;
        if (sp != null && !sp.equals("")) {
            page = Integer.parseInt(sp);
        }
        List<NewsBean> news = newsDao.getList(page);
        req.setAttribute("news",news);
        // 4 显示页面 网页重定向显示
        req.getRequestDispatcher("WEB-INF/news.jsp").forward(req,resp);

    }
}
